package roadsuggester.main;


import java.util.ArrayList;

import roadsuggester.db.UserLocationsDatabase;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.Toast;


//Main Activity
public class RoadSuggesterActivity extends Activity
{
	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		
		
		populateLocations();

	}

	public void onAddNewLocationButtonPressed(View view)
	{
		Intent intent=new Intent(RoadSuggesterActivity.this, AddNewLocation.class);
		startActivityForResult(intent, IntentRequestCodes.ADD_NEW_LOCATION_ACTION);

	}

	public void populateLocations()
	{

		final ListView listView=(ListView) findViewById(R.id.userLocationsListView);

		UserLocationsDatabase db=UserLocationsDatabase.getInstance(RoadSuggesterActivity.this);
		Cursor c=db.getLocationsCursor();
		ListAdapter adapter=new SimpleCursorAdapter(this, R.layout.user_locations_listview_element, c, new String[] { "label",
				"address" }, new int[] { R.id.user_location_label, R.id.user_location_address });


		
		
		listView.setAdapter(adapter);

		listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener()
		{
			@Override
			public boolean onItemLongClick(AdapterView<?> av, View v, int pos, long id)
			{
				onListItemLongClick(id);
				return true;
			}
		});

		
		listView.setOnItemClickListener(new OnItemClickListener()
		{
			@Override
			public void onItemClick(AdapterView<?> av, View v, int pos, long id)
			{
				Intent intent=new Intent(RoadSuggesterActivity.this, TrackerMap.class);
				intent.putExtra("destinationID", id);

				startActivityForResult(intent, IntentRequestCodes.RUN_TRACKER_MAP_ACTION);
			}
		});


	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data)
	{
		super.onActivityResult(requestCode, resultCode, data);

		if(resultCode==IntentResultCodes.UPDATE_DATABASE)
		{
			populateLocations();
		}
	}

	private void onListItemLongClick(final long id)
	{
		final CharSequence[] items= { "Edit", "Delete", "Cancel" };

		AlertDialog.Builder builder=new AlertDialog.Builder(this);
		builder.setTitle("Pick an action");
		builder.setItems(items, new DialogInterface.OnClickListener()
		{
			public void onClick(DialogInterface dialog, int item)
			{
				if(item==0)
				{
					Intent intent=new Intent(RoadSuggesterActivity.this, AddNewLocation.class);


					intent.putExtra("editMode", true);
					intent.putExtra("editID", id);

					startActivityForResult(intent, IntentRequestCodes.ADD_NEW_LOCATION_ACTION);

				}
				else if(item==1)
				{
					UserLocationsDatabase db=UserLocationsDatabase.getInstance(RoadSuggesterActivity.this);
					db.deleteLocation(id);
					db.close();
					populateLocations();
				}
				else
				{
					dialog.cancel();
				}
			}
		});

		AlertDialog alert=builder.create();
		alert.show();
	}
	
	

}